<?php
/**
 * Created by allen.qiang.
 * 大众点评霸王餐活动ajax
 * Date: 14-12-25
 * @author za.yu allen.qiang@fistforward.com
 * FUN-AC-0025
 */
quanli_require('module/base/customer/quanliCust.class.php');
quanli_require('module/base/activity/activitySpike.class.php');
quanli_require('module/base/shop/shopCart.class.php');
quanli_require('module/base/activity/activityConfig.class.php'); //活动配置类
quanli_require('module/base/activity/activityKingMeal.class.php'); //霸王餐活动类

class ajax_kingMeal{

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //会员注册
                    $this->register();
                    break;
                case 2: //提交“霸王餐”订单
                    $this->bill_submit();
                    break;
                case 3: //发送手机验证码
                    $this->sendSms();
                    break;
                default:
                    returnJsonData('参数错误');
                    break;


            }

        } else {
            returnJsonData('参数错误');
        }

    }

    /**
     * 发送手机验证码
     *  @author  20141208 allen.qiang <allen.qiang@fistforward.com>
     */
    private function sendSms() {

        global $G_SHOP;

        //加载短信类
        quanli_include('module/base/common/quanliSms.class.php');

        //实例化短信类
        $quanliSms = new quanliSms();

        if (!isset($_REQUEST['mobile'], $_REQUEST['template']) || !is_mobile($_REQUEST['mobile']) || empty($_REQUEST['template'])) {
            returnJsonData('参数错误');
        }

        //实例化霸王餐活动类
        $activityKingMeal=new activityKingMeal();
        $KingMeal=$activityKingMeal->activity_king_meal($_REQUEST);//霸王餐活动配置信息
        if($KingMeal["activity_authority"]==0){
            returnJsonData('抱歉，您无法参与本活动');
        }

        $mobile = $_REQUEST['mobile']; //手机号码
        $template = $_REQUEST['template']; //消息模板

        $current_time = time(); //当前时间戳
        $last_send_time = $G_SHOP->sessionCache_get('sms_yzm_time_' . $mobile); //上一次发放短信的时间戳
        if (!empty($last_send_time) && $current_time - $last_send_time < 60) {
            returnJsonData('手机短信已发送，请一分钟以后再试');
        }


        /**
         * ============================
         * 发送短信
         * ============================
         */

        //生成短信验证码
        $sms_yzm = $quanliSms->createSmsCode();

        //生成短信内容
        $data_array = array(
            '$sms_yzm' => $sms_yzm,
            '$mobile' => $mobile
        );
        $sms_content = $quanliSms->getSmsContent( $template, $data_array );
        if (!$sms_content) {
            returnJsonData('短信初始化失败', 404);
        }

        //发送短信
        $result_array = $quanliSms->sendSMS( $mobile, $sms_content );

        /**
         * ============================
         * 发送短信
         * ============================
         */

        if ($result_array['code'] == 200) {

            //保存短信验证码到redis
            $G_SHOP->sessionCache_set('sms_yzm_' . $mobile, md5($sms_yzm), 3600);//将验证码信息保存到memcache中去
            $G_SHOP->sessionCache_set('sms_yzm_time_' . $mobile, time(), 3600);

        }

        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 提交“霸王餐”订单
     *  @author  20141205 allen.qiang <allen.qiang@fistforward.com>
     */
    private function bill_submit()
    {
        global $G_SHOP;
        $rs_array = array(
            'code'=>200,
            'message'=>'ok'
        );

        //实例化霸王餐活动类
        $activityKingMeal=new activityKingMeal();
        $_REQUEST["mobile"]=$_REQUEST['deliver_mobile'];
        $KingMeal=$activityKingMeal->activity_king_meal($_REQUEST);//霸王餐活动配置信息
        if($KingMeal["activity_isReceive"]==1){
            returnJsonData('您已兑换过此商品!');
        }

        //实例化霸王餐活动类
        $activityKingMeal=new activityKingMeal();

        $send_time_array = $_REQUEST['send_time'];
        $send_time_start = mb_substr($send_time_array,3,5).':00';
        $send_time_end = mb_substr($send_time_array,9,5).':00';


        if( empty( $_REQUEST['deliver_mobile'] ) )
        {
            returnJsonData('请填写收货人手机!');
        }

        if( empty( $_REQUEST['deliver_name'] ) )
        {
            returnJsonData('请填写收货人姓名!');
        }


        if($_REQUEST['country']==0)
        {
            returnJsonData('请选择收货地址的区信息!');
        }

        if( empty( $_REQUEST['deliver_address'] ) )
        {
            returnJsonData('请填写收货地址!');
        }

        $bill = array();

        $bill['custID'] = $G_SHOP->currentCustomerID;

        $bill['country'] = $_REQUEST['country']; //区id
        $bill['deliver_name'] = $_REQUEST['deliver_name']; //收货人
        $bill['deliver_address'] = $_REQUEST['deliver_address'];//收货人地址
        $bill['deliver_mobile'] = $_REQUEST['deliver_mobile'];//收货人手机

        $bill['is_the_invoice'] = $_REQUEST['is_the_invoice'];
        $bill['invoice_title_type'] = $_REQUEST['invoice_title_type'];//1：个人 2：单位
        $bill['invoice_title'] = $_REQUEST['invoice_title']; //发票抬头


        $bill['payTypeID'] = $_REQUEST['payTypeID']; //支付方式ID
        $bill['payment_method_id'] = 58; //付款方式


        $bill['send_date'] = $_REQUEST['send_date'];
        $bill['send_start_time'] = $send_time_start;
        $bill['send_end_time'] = $send_time_end;

        $bill['billPromotion'] = $KingMeal['billPromotion'];//促销渠道
        $bill["sku"]=$KingMeal["sku"]; //活动商品sku
        $bill["billChannel"]=$_REQUEST['billChannel']; //订单渠道

        $result_array = $activityKingMeal->create_bill($bill);
        //error_log(print_r($result_array,1));
        $rs_array['code'] = $result_array['code'];
        $rs_array['message'] = $result_array['message'];
        if($result_array['code']==200)
        {
            $rs_array['bill_no'] = $result_array['bill_no'];
            returnJsonData($rs_array['bill_no'],$rs_array['code']);
        }


        returnJsonData($rs_array['message'],$rs_array['code']);
    }


    /**
     * 注册会员信息
     *  @author  20141205 allen.qiang <allen.qiang@fistforward.com>
     */
    private function register() {
        $activityKingMeal=new activityKingMeal(); //霸王餐活动配置信息
        $result_array= $activityKingMeal->register();
        returnJsonData($result_array['message'], $result_array['code']);
    }


}